package com.pan.admin.module.distribute.controller;

import com.pan.admin.common.constant.module.StatisticEnum;
import com.pan.admin.common.constant.module.StatisticEnumVisitEnum;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.module.distribute.entity.LoanDistributeStatistic;
import com.pan.admin.module.distribute.entity.qo.LoanDistributeStatisticQO;
import com.pan.admin.module.distribute.entity.vo.LoanDistributeStatisticVO;
import com.pan.admin.module.distribute.service.LoanDistributeStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 智能分配统计Controller
 *
 * @author ts
 * @date 2023-09-10
 */
@RestController
@RequestMapping("/distribute/statistic")
public class LoanDistributeStatisticController extends BaseController {

    @Autowired
    private LoanDistributeStatisticService loanDistributeStatisticService;

    /**
     *  根据链接组+子链接
     *  <br>维度已子链接为准</>
     */
    @PreAuthorize("@ss.hasPermi('distribute:statistic:list')")
    @GetMapping("/listLink")
    public AjaxResult listLink(@RequestParam("groupId") Long groupId,
                               @RequestParam("type") String type,
                               @RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanDistributeStatisticQO statisticQO = statisticEnum.getDistributeStatisticQO(groupId);

        List<LoanDistributeStatistic> list = null;
        if (StatisticEnumVisitEnum.UV.getType().equals(type)) {
            list = loanDistributeStatisticService.listLinkUv(statisticQO);
        } else {
            list = loanDistributeStatisticService.listLinkPv(statisticQO);
        }

        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }

        //  转化图表数据
        if (StatisticEnum.NOW.getType().equals(viewType)) {
            return AjaxResult.success(list);
        }

        //  TODO 因为子链接删除了，在数据库表中不存在，但是统计数据还存在，暂时先删除
        list = list.stream().filter(t -> t.getName() != null).collect(Collectors.toList());
        //  转化统计图表实体
        List<LoanDistributeStatisticVO> fill = LoanDistributeStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     *  根据链接组统计
     *  <br>维度已链接组为准</>
     */
    @PreAuthorize("@ss.hasPermi('distribute:statistic:list')")
    @GetMapping("/listGroup")
    public AjaxResult listGroup(@RequestParam("groupId") Long groupId,
                               @RequestParam("type") String type,
                               @RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanDistributeStatisticQO statisticQO = statisticEnum.getDistributeStatisticQO(groupId);

        List<LoanDistributeStatistic> list = null;
        if (StatisticEnumVisitEnum.UV.getType().equals(type)) {
            list = loanDistributeStatisticService.listGroupUv(statisticQO);
        } else {
            list = loanDistributeStatisticService.listGroupPv(statisticQO);
        }

        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }

        //  转化图表数据
        if (StatisticEnum.NOW.getType().equals(viewType)) {
            return AjaxResult.success(list);
        }

        //  TODO 因为子链接删除了，在数据库表中不存在，但是统计数据还存在，暂时先删除
        list = list.stream().filter(t -> t.getName() != null).collect(Collectors.toList());
        //  转化统计图表实体
        List<LoanDistributeStatisticVO> fill = LoanDistributeStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     *  根据链接组ID统计子链接总和
     */
    @PreAuthorize("@ss.hasPermi('distribute:statistic:list')")
    @GetMapping("/listGroupLinkSum")
    public TableDataInfo listGroupLinkSum(@RequestParam("groupId") Long groupId, @RequestParam("type") String type) {
//        LoanChannelStatisticQO loanDistributeStatisticQO = new LoanChannelStatisticQO();
//        loanDistributeStatisticQO.setGroupId(groupId);
//
//        if (StatisticEnumVisitEnum.UV.getType().equals(type)) {
//            return getDataTable(loanDistributeStatisticService.listLinkUv(loanDistributeStatisticQO));
//        }
//        return getDataTable(loanDistributeStatisticService.listLinkPv(loanDistributeStatisticQO));

        return null;
    }

    /**
     *  统计链接组统计列表
     */
    @PreAuthorize("@ss.hasPermi('distribute:statistic:list')")
    @GetMapping("/listGroupSum")
    public TableDataInfo listGroupSum(@RequestParam("type") String type) {
        return null;
//        if (StatisticEnumVisitEnum.UV.getType().equals(type)) {
//            return getDataTable(loanDistributeStatisticService.listGroupUvSum());
//        }
//        return getDataTable(loanDistributeStatisticService.listGroupPvSum());
    }

}
